package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.DzBqvalue;
import com.ruoyi.system.mapper.DzBqtypeMapper;
import com.ruoyi.system.domain.DzBqtype;
import com.ruoyi.system.service.IDzBqtypeService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 标签类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-14
 */
@Service
public class DzBqtypeServiceImpl implements IDzBqtypeService 
{
    @Autowired
    private DzBqtypeMapper dzBqtypeMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询标签类型
     * 
     * @param blId 标签类型主键
     * @return 标签类型
     */
    @Override
    public DzBqtype selectDzBqtypeByBlId(Long blId)
    {
        return dzBqtypeMapper.selectDzBqtypeByBlId(blId);
    }

    /**
     * 查询标签类型列表
     * 
     * @param dzBqtype 标签类型
     * @return 标签类型
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<DzBqtype> selectDzBqtypeList(DzBqtype dzBqtype)
    {

        //权限控制
        return dzBqtypeMapper.selectDzBqtypeList(dzBqtype);
    }

    @Override
    public List<DzBqtype> selectDzBqtypeListWeb(DzBqtype dzBqtype)
    {
        return dzBqtypeMapper.selectDzBqtypeList(dzBqtype);
    }

    /**
     * 新增标签类型
     * 
     * @param dzBqtype 标签类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDzBqtype(DzBqtype dzBqtype)
    {
        int rows = dzBqtypeMapper.insertDzBqtype(dzBqtype);
        insertDzBqvalue(dzBqtype);
        return rows;
    }

    /**
     * 修改标签类型
     * 
     * @param dzBqtype 标签类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDzBqtype(DzBqtype dzBqtype)
    {
        dzBqtypeMapper.deleteDzBqvalueByBlId(dzBqtype.getBlId());
        insertDzBqvalue(dzBqtype);
        return dzBqtypeMapper.updateDzBqtype(dzBqtype);
    }

    /**
     * 批量删除标签类型
     * 
     * @param blIds 需要删除的标签类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDzBqtypeByBlIds(Long[] blIds)
    {
        dzBqtypeMapper.deleteDzBqvalueByBlIds(blIds);
        return dzBqtypeMapper.deleteDzBqtypeByBlIds(blIds);
    }

    /**
     * 删除标签类型信息
     * 
     * @param blId 标签类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDzBqtypeByBlId(Long blId)
    {
        dzBqtypeMapper.deleteDzBqvalueByBlId(blId);
        return dzBqtypeMapper.deleteDzBqtypeByBlId(blId);
    }

    /**
     * 新增标签属性信息
     * 
     * @param dzBqtype 标签类型对象
     */
    public void insertDzBqvalue(DzBqtype dzBqtype)
    {
        List<DzBqvalue> dzBqvalueList = dzBqtype.getDzBqvalueList();
        Long blId = dzBqtype.getBlId();
        if (StringUtils.isNotNull(dzBqvalueList))
        {
            List<DzBqvalue> list = new ArrayList<DzBqvalue>();
            for (DzBqvalue dzBqvalue : dzBqvalueList)
            {
                dzBqvalue.setBlId(blId);
                list.add(dzBqvalue);
            }
            if (list.size() > 0)
            {
                dzBqtypeMapper.batchDzBqvalue(list);
            }
        }
    }
}
